package xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Text {
    //探讨产生对象生成对象
    public static void a(){
        //1.加载xml文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.获取对象
        //得到一个名称为a1的对象 byName:缺点是需要强转
        DemoA a1 = (DemoA) context.getBean("a1");
        // 得到一个类型是DemoA的对象 byType:缺点是只能有一个对象
        //DemoA a2 = context.getBean(DemoA.class);
        // 根据名称和类型获取对象 byName+byType
        DemoA a3 = context.getBean("a2", DemoA.class);
        a1.a();
        //a2.a();
        a3.a();
    }
    //探讨给对象赋值
    public static void b(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       DemoB b = context.getBean(DemoB.class);
        DemoC c = context.getBean(DemoC.class);
        DemoD d = context.getBean(DemoD.class);
        System.out.print(b);
        System.out.println(c);
        System.out.println(d);
    }
    public static void c(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoE e1 = context.getBean("e3", DemoE.class);
        System.out.println(e1);
    }
    public static void d(){
        // 在加载xml时 已经产生了对象 默认情况是：勤加载方式
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoF f = context.getBean("f", DemoF.class);
    }
    public static void e(){
        // 默认情况下：单例模式
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoG g1 = context.getBean("g", DemoG.class);
        DemoG g2 = context.getBean("g", DemoG.class);
        System.out.println(g1);
        System.out.println(g2);
        System.out.println(g1==g2);
    }
    public static void main(String[] args) {
      d();
    }
}
